package com.ve.c51;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.ve.compiler.Compiler;
import com.ve.compiler.ExampleInstaller;
import com.ve.compiler.Installer;
import com.ve.compiler.ProgressListener;
import com.ve.file.GFile;

public class SplashActivity extends AppCompatActivity {

    private static final String TAG = SplashActivity.class.getSimpleName();
    private Installer installer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        if (hasPermission()) {
            init();
        }
    }

    private void init() {
        ExampleInstaller.install(this);
        GFile.init(this);
        Log.d(TAG, "init: ");
        installer = new Installer(this);
        if (installer.isInstalled()) {
            boot();
        } else {
            installer.setProgressListener(new ProgressListener.DialogProgressAdapter(this, "安装环境") {
                @Override
                public void onSucceed(String info) {
                    super.dismissDialog();
                    boot();
                }

            });
            installer.setup();
        }
    }

    private void boot() {
        Log.d(TAG, "boot: ");
        Compiler.getCompiler().boot(SplashActivity.this);
        if (Compiler.getCompiler().isBooted()) {
            startActivity(new Intent(this, MainActivity.class));
            finish();
        } else {
            new AlertDialog.Builder(this)
                    .setTitle("失败")
                    .setMessage("启动编译环境失败")
                    .show();
            startActivity(new Intent(this, ConsoleActivity.class));
            finish();
        }

    }


    private boolean hasPermission() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return true;
        }
        String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
        boolean has = true;
        for (String permission : permissions) {
            if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
                has = false;
                break;
            }
        }
        if (!has) {
            requestPermissions(permissions, 999);
        }
        return has;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        for (int i = 0; i < grantResults.length; i++) {
            if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "授权失败", Toast.LENGTH_LONG).show();
                return;
            }
        }
        init();
    }


}
